<?php

require_once '../util/pdo.php';

$return = ['msg'=>"空！", 'state'=>'0'];

switch ($_GET['source']) {

    // 登录
    case "login":

        // 获取数据
        $data = $_POST;

        // 去除两边空格
        $username = trim($data['username']);
        $password = trim($data['password']);

        // 校验数据
        if (strlen($username) <= 0 || strlen($username) > 30 ) {
            echo json_encode(['msg'=>"名字不能为空，或长度不对！", 'state'=>'0']);
            die;
        }
        if (strlen($password) < 6 || strlen($password) > 16 ) {
            echo json_encode(['msg'=>"密码不能为空，或长度不对！", 'state'=>'0']);
            die;
        }

        $password = md5(md5($password));

        $pdo = new pdo_util();
        $userInfo = $pdo->select('user', '*', ['name'=>$username, 'pwd'=>$password, 'status'=>1]);

        if (empty($userInfo)) {
            echo json_encode(['msg'=>"用户名和密码错误， 或，请注册重试！", 'state'=>'0']);
            die;
        }

        $return = ['msg'=>"登录成功", 'state'=>'1', 'data'=>['id'=>current($userInfo)['id']]];
        break;
    // 注册
    case "register":

        // 获取数据
        $data = $_POST;

        // 去除两边空格
        $username   = trim($data['username']);
        $phone      = trim($data['phone']);
        $password   = trim($data['password']);
        $repassword = trim($data['repassword']);

        if (strlen($username) <= 0 || strlen($username) > 30 ) {
            echo json_encode(['msg'=>"名字不能为空，或长度不对！", 'state'=>'0']);
            die;
        }

        if(!preg_match("/^1[345789]\d{9}$/", $phone)) {
            echo json_encode(['msg'=>"手机号不正确，请重新输入", 'state'=>'0']);
            die;
        }

        if (strlen($password) < 6 || strlen($password) > 16 ) {
            echo json_encode(['msg'=>"密码不能为空，或长度不对！", 'state'=>'0']);
            die;
        }

        if ($password != $repassword) {
            echo json_encode(['msg'=>"密码输入不正确，请重试", 'state'=>'0']);
            die;
        }

        // 查询账号之前是否注册
        $pdo = new pdo_util();
        $userInfo = $pdo->select('user', '*', ['status' => 1, 'name' =>$username]);
        if (!empty($userInfo)) {
            echo json_encode(['msg'=>"当前用户已注册", 'state'=>'0']);
            die;
        }

        $password = md5(md5($password));
        $result = $pdo->add('user', ['name'=>$username, 'pwd'=>$password, 'phone'=>$phone]);

        if (empty($result)) {
            echo json_encode(['msg'=>"用户名和密码错误， 或，请注册重试！", 'state'=>'0']);
            die;
        }

        $return = ['msg'=>"注册成功", 'state'=>'1'];

        break;

    // 编辑
    case "edit":

        $username= $_POST['username'];
        $phone   = $_POST['phone'];
        $id      = $_POST['id'];

        if (strlen($username) <= 0 || strlen($username) > 30 ) {
            echo json_encode(['msg'=>"名字不能为空，或长度不对！", 'state'=>'0']);
            die;
        }

        if(!preg_match("/^1[345789]\d{9}$/", $phone)) {
            echo json_encode(['msg'=>"手机号不正确，请重新输入", 'state'=>'0']);
            die;
        }

        $pdo = new pdo_util();
        $result = $pdo->update('user', ['name'=>$username, 'phone'=>$phone], ['id'=>$id]);

        if (!$result) {
            echo json_encode(['msg'=>"修改失败", 'state'=>'0']);
            die;
        }
        $return = ['msg'=>"修改成功", 'state'=>'1'];

        break;

    default:
        $return = ['msg'=>"空！", 'state'=>'0'];
}


echo json_encode($return);